<?php
/**
 * Created by PhpStorm.
 * User: xujm
 * Date: 2016/8/9
 * Time: 18:55
 */

use Phalcon\Mvc\Controller;
use Phalcon\Mvc\Model\Query;

class NavController extends Controller {

    public function indexAction(){
        $this->view->nav = Nav::find();

    }
    public function addAction(){
        $nav = new Nav();
        $this->view->nav_cat = Navcat::find();
        $this->view->nav_list = Nav::find();
        $this->view->article = Article::find();

        $success = $nav->save(
            $this->request->getPost(),
            array('cid','parentid','label','target','href','status')
        );
        if ($success) {
            echo "保存成功";
        } else {
            echo "保存失败 ";
            foreach ($nav->getMessages() as $message) {
                echo $message->getMessage(), "<br/>";
            }
        }

    }


    public function editAction(){
        $nav = new Nav();
        $nav_list = Nav::find(
            array(
                "id" => $this->request->getPost('id')
            )
        );
        if (!$nav_list){
            $this->redirect($this->url->get() . 'nav/index');
        }

        //var_dump($this->request->getPost('id'));die();
        if ($this->request->isPost()){
            $success = $nav->update(
                $this->request->getPost(),
                array('cid','parentid','label','target','href','status')
            );
            if ($success) {
                echo "更新成功";
            } else {
                echo "更新失败 ";
                foreach ($nav->getMessages() as $message) {
                    echo $message->getMessage(), "<br/>";
                }
            }
        }



    }


    public function deleteAction()
    {
        if (!$this->request->getPost() || !$this->request->isAjax()) {
            return $this->flash->error('post ajax required');
        }

        $nav_id = $this->request->getPost('nav_id');

        $model = Nav::findFirst($nav_id);
        if ($model) {
            if ($model->delete()) {
                $this->returnJSON([
                    'success' => true
                ]);
            }
        }
    }

}